package com.xnx3.wangmarket.plugin.tcdnAutoGenerate;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.xnx3.j2ee.pluginManage.PluginRegister;
import com.xnx3.j2ee.util.AttachmentUtil;
import com.xnx3.j2ee.util.ConsoleUtil;
import com.xnx3.j2ee.util.SpringUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.Func;
import com.xnx3.wangmarket.admin.bean.NewsDataBean;
import com.xnx3.wangmarket.admin.cache.TemplateCMS;
import com.xnx3.wangmarket.admin.entity.News;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.entity.SiteColumn;
import com.xnx3.wangmarket.admin.pluginManage.interfaces.GenerateSiteInterface;
import com.xnx3.wangmarket.plugin.tcdnAutoGenerate.entity.Tcdn;

import cn.zvo.http.Http;
import cn.zvo.http.Response;

/**
 * @author 管雷鸣
 *
 */
@PluginRegister(menuTitle="TCDN翻译", menuHref = "/plugin/tcdn/index.jsp", intro="对接 translate.admin 的 自动执行翻译任务的api接口，当网站点击生成整站，在生成完成后，会继续等待2分钟，然后触发执行翻译任务的API接口。 这个API接口是每个网站管理员自己所设置的。", version="1.0", versionMin="5.7", applyToCMS = true)
public class Plugin implements GenerateSiteInterface{
	@Override
	public void generateSiteFinish(HttpServletRequest request, Site site, Map<String, SiteColumn> siteColumnMap,
			Map<String, List<News>> newsMap, Map<Integer, NewsDataBean> newsDataMap, TemplateCMS template) {
		//当用户点击生成整站后，在10秒后自动调用这个api接口
		
		Tcdn tcdn = SpringUtil.getSqlService().findById(Tcdn.class, site.getId());
		if(tcdn == null || tcdn.getIsUse() == null || tcdn.getIsUse() - Tcdn.ISUSE_FALSE == 0 || tcdn.getUrl() == null || tcdn.getUrl().length() < 3) {
			return;
		}
		
		
		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(10*1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				Http http = new Http();
				try {
					Response res = http.get(tcdn.getUrl());
					ConsoleUtil.debug("tcdn plugin auto generate, siteid:"+site.getId()+", response:"+res.getCode()+", "+res.getContent());
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}).start();
	}
	

	@Override
	public BaseVO generateSiteBefore(HttpServletRequest request, Site site) {
		// TODO Auto-generated method stub
		return null;
	}


}
